package com.example.ssss;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.ListView;
import android.widget.TextView;

import com.example.ssss.data.MysqlOperation;
import com.example.ssss.data.entity.ShareData;
import com.example.ssss.databinding.ActivityContactBinding;
import com.example.ssss.databinding.FragmentHomeBinding;
import com.example.ssss.entity.ContactInfo;
import com.example.ssss.entity.HistoryInfo;
import com.example.ssss.ui.home.HomeFragment;

import java.util.Arrays;

public class ContactActivity extends AppCompatActivity {
    private ActivityContactBinding binding;
    private Button addBtn;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_contact);
        String userid;

        ShareData shareData=(ShareData) getApplication();
        userid=shareData.userId;
        select(userid);
//        ListView clv = findViewById(R.id.contact_list);
//        // 添加ListItem，设置事件响应
//        clv.setAdapter(new ContactListAdapter());
//        clv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
//            public void onItemClick(AdapterView<?> arg0, View v, int index,
//                                    long arg3) {
//                onListItemClick(index);
//            }
//        });
        addBtn = findViewById(R.id.contactAddBtn);
        addBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                showEditDialog(0);
            }
        });

    }

    void showEditDialog(int index){//index 用来区分是添加还是修改联系人
        final ContactEditDialog contactEditDialog = new ContactEditDialog(this);

        contactEditDialog.setOnCancelOnclickListener(new ContactEditDialog.onCancelOnclickListener() {
            @Override
            public void onCancelClick() {
                contactEditDialog.dismiss();
            }
        });

        contactEditDialog.show();
        Window window = contactEditDialog.getWindow();
        window.setGravity(Gravity.CENTER);
        //window.setWindowAnimations(R.style.popupAnimation);
        contactEditDialog.setCanceledOnTouchOutside(true);
        contactEditDialog.setCancelable(true);
    }

    void onListItemClick(int index) {
        //TODO 弹出修改联系人弹窗
        showEditDialog(index);
    }

    private static ContactInfo[] demos = {
            new ContactInfo("1","2","3"),
            new ContactInfo("1","2","3"),
            new ContactInfo("1","2","3")
//            new ContactInfo(R.string.contact1_relationship,R.string.contact1_name,R.string.contact1_tel),
//            new ContactInfo(R.string.contact2_relationship,R.string.contact2_name,R.string.contact2_tel)
    };

    private class ContactListAdapter extends BaseAdapter {
        public ContactListAdapter() {
            super();
        }
        @Override
        public View getView(int index, View convertView, ViewGroup parent) {
            convertView = View.inflate(getApplicationContext(), R.layout.contact_list_item, null);
            TextView name = (TextView) convertView.findViewById(R.id.contact_name);
            TextView relationship = (TextView) convertView.findViewById(R.id.contact_relationship);
            TextView tel = (TextView) convertView.findViewById(R.id.contact_tel);

            name.setText(demos[index].getName());
            relationship.setText(demos[index].getRelationship());
            tel.setText(demos[index].getTel());

            return convertView;
        }

        @Override
        public int getCount() {
            return demos.length;
        }

        @Override
        public Object getItem(int index) {
            return demos[index];
        }

        @Override
        public long getItemId(int id) {
            return id;
        }
    }

//    private static class ContactInfo {
//        private final int relationship;
//        private final int name;
//        private final int tel;
//
//        public ContactInfo(int relationship,int name,int tel) {
//            this.name=name;
//            this.relationship=relationship;
//            this.tel=tel;
//        }
//    }
        private Handler handler=new Handler(){
            @Override
            public void handleMessage(Message msg){
                    demos= (ContactInfo[]) msg.obj;
                    ListView mListView = (ListView) findViewById(R.id.contact_list);
                    System.out.println(Arrays.toString(demos));
                    mListView.setAdapter(new ContactListAdapter());
                    mListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
                        public void onItemClick(AdapterView<?> arg0, View v, int index,
                                    long arg3) {
                                onListItemClick(index);
                        }
                    });
            }
        };
    public void select(String userid){
        new Thread(){
            @Override
            public void run() {
                super.run();
                MysqlOperation sql = new MysqlOperation();
                sql.contactDao=new MysqlOperation.ContactDao();
                try {
//                    demos =sql.historyDao.select("history_item");
                    demos =sql.contactDao.select(userid);
                    System.out.println("Mysql连接成功");
                    Message message=new Message();
                    message.obj=demos;
                    handler.sendMessage(message);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        }.start();
    }

    public void update(String userid,int index,String contact_name,String relationship,String phone){
        new Thread(){
            @Override
            public void run() {
                super.run();
                MysqlOperation sql = new MysqlOperation();
                sql.contactDao=new MysqlOperation.ContactDao();
                try {

                    sql.contactDao.update(userid,index,contact_name,relationship,phone);
                    System.out.println("Mysql连接成功");

                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        }.start();
    }

    public void insert(String userid,String contact_name,String relationship,String phone){
        new Thread(){
            @Override
            public void run() {
                super.run();
                MysqlOperation sql = new MysqlOperation();
                sql.contactDao=new MysqlOperation.ContactDao();
                try {

                    sql.contactDao.insert(userid,contact_name,relationship,phone);
                    System.out.println("Mysql连接成功");

                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        }.start();
    }
}